//
//  BattleMeteoriteLayer.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-4-10.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "BattleMeteoriteLayer.h"
#import "Variable.h"

@implementation BattleMeteoriteLayer


+(id) meteoriteWithType:(BOOL) isFarMeteorite
{
     return [[[self alloc] initMeteoriteWithType:isFarMeteorite] autorelease];
}

-(id) initMeteoriteWithType:(BOOL) isFarMeteorite
{
    self = [super init]; 
	
	if (self!= nil) 
    {
        size = [[CCDirector sharedDirector] winSize];
        
        _isFarMeteorite = isFarMeteorite;
        
        satrDict = [NSMutableDictionary dictionaryWithCapacity:500];
        
        posDict = [NSMutableDictionary dictionaryWithCapacity:500];
        
        [satrDict retain];
        
        [posDict retain];
        
        [self initStars:1.0 starNum:500];
        
//        self.anchorPoint = ccp(0, 0);
    }
    
    return self;
}

/**
	初始化星星
 */
-(void) initStars:(float) scale starNum:(int) starNum
{
    if (_isFarMeteorite)//远陨石层
    {
        for (int i = 0; i < starNum; i++) 
        {
            CCSprite* starSp = [CCSprite spriteWithSpriteFrameName:@"starSmall.png"]; 
            float starX = CCRANDOM_0_1() * screenMaxWidth;
            float starY = CCRANDOM_0_1() * screenMaxHeight;
            starSp.position = ccp(starX, starY);
            [self addChild:starSp];
            [satrDict setObject:starSp forKey:[NSString stringWithFormat:@"%d", i]];
            [posDict setObject:[NSValue valueWithCGPoint:ccp(starX, starY)] forKey:[NSString stringWithFormat:@"%d", i]];
//            CCLOG(@"添加一个新的星星，位置为X:%fY:%f", starX, starY);
        }
    }
    else//近陨石层
    {
        for (int i = 0; i < starNum; i++) 
        {
            CCSprite* starSp = [CCSprite spriteWithSpriteFrameName:@"starBig.png"]; 
            float starX = CCRANDOM_0_1() * screenMaxWidth;
            float starY = CCRANDOM_0_1() * screenMaxHeight;
            starSp.position = ccp(starX, starY);
            [self addChild:starSp];
            [satrDict setObject:starSp forKey:[NSString stringWithFormat:@"%d", i]];
            [posDict setObject:[NSValue valueWithCGPoint:ccp(starX, starY)] forKey:[NSString stringWithFormat:@"%d", i]];
//            CCLOG(@"添加一个新的星星，位置为X:%fY:%f", starX, starY);
        }
    }
}

/**
	处理视图模式改变
    微观视图(1/1)、局部视图(1/3)、宏观视图(1/9)、全局视图(1/27)
 */
-(void) updateViewType:(BattleViewType) changeToType
{   
    if (_isFarMeteorite)//远陨石层
    {
        switch (viewType) 
        {
            case BattleViewMicroscopic://微观视图(1/1)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x, oldPos.y);
                }
               break; 
            }
            case BattleViewLocal://局部视图(1/3)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x / 3, oldPos.y / 3);
                }
                break; 
            }
            case BattleViewMacroscopic://宏观视图(1/9)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x / 9, oldPos.y / 9);
                }
                break; 
            }
            case BattleViewOverallSituation://全局视图(1/27)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x / 27, oldPos.y / 27);
                }
                break; 
            }
            default:
                break;
        }
        CCLOG(@"远陨石层：处理视图模式改变");
    }
    else//近陨石层
    {
        switch (viewType) 
        {
            case BattleViewMicroscopic://微观视图(1/1)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x, oldPos.y);
                }
                break; 
            }
            case BattleViewLocal://局部视图(1/3)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x / 3, oldPos.y / 3);
                }
                break; 
            }
            case BattleViewMacroscopic://宏观视图(1/9)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x / 9, oldPos.y / 9);
                }
                break; 
            }
            case BattleViewOverallSituation://全局视图(1/27)
            {
                for (NSString*keyStr in satrDict) 
                {
                    CCSprite* starSp = [satrDict objectForKey:keyStr];
                    NSValue* vp = [posDict objectForKey:keyStr];
                    CGPoint oldPos = [vp CGPointValue];
                    starSp.position = ccp(oldPos.x / 27, oldPos.y / 27);
                }
                break; 
            }
            default:
                break;
        }
        CCLOG(@"近陨石层：处理视图模式改变");
    }
    
}

@end
